//
//  SAInventoryVC.m
//  yidiantong
//
//  Created by 黄志刚 on 2020/10/9.
//

#import "SAInventoryVC.h"
#import "SAInventoryCell.h"
#import "SAInventoryListModel.h"
#import "SASupplierScreeningView3.h"
#import "SAStockPreviewListAPI.h"
#import "SAInventoryDetailVC.h"
#import "SAInventorySortView.h"
#import "SAInventoryWarnVC.h"
#import "SAproductGetAppConditionAPI.h"
#import "YBImageBrowser.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "SAWarehousingVC.h"
#import "ScanHelper.h"

@interface SAInventoryVC ()<UITableViewDataSource, UITableViewDelegate, HTTPAPIDelegate, UITextFieldDelegate,YBImageBrowserDataSource>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UITextField *searchTf;
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIButton *moreBtn;
@property (nonatomic, strong) UIView *moreView;
@property (nonatomic, strong) UIButton *screeningBtn;
@property (nonatomic, strong) UIView *screeningLine;
@property (nonatomic, strong) UIButton *sortingBtn;
@property (nonatomic, strong) UIView *sortingLine;
@property (nonatomic, strong) UIButton *detailBtn;
@property (nonatomic, strong) UIView *detailLine;
@property (nonatomic, strong) UIButton *warningBtn;
@property (nonatomic, strong) UIView *warningLine;
@property (nonatomic, assign) NSInteger currentPage;

@property (nonatomic, strong) NSMutableArray<SAInventoryListRecods *> *dataMarr;
@property (nonatomic, strong) NSMutableArray *screenArray;
@property (nonatomic, strong) NSString *providerId;
@property (nonatomic, strong) NSString *providerName;
@property (nonatomic, strong) UIButton *topBtn;
@property (nonatomic, strong) UILabel *lineNumLab;
@property (nonatomic, strong) UIView *lineborderView;
@property (nonatomic, strong) SAStockPreviewListAPI *stockPreviewListAPI;
@property (nonatomic, strong) SAproductGetAppConditionAPI *productGetAppConditionAPI;
@property (nonatomic, assign) NSInteger sortNum;
@property (nonatomic, strong) UIView *tipView;
@property (nonatomic, strong) NSArray *imageList;
@property (nonatomic, strong) UILabel *totalLab;
@property (nonatomic, strong) UIButton *warehousingBtn;
@property (nonatomic, strong) UIButton *scanBtn;

@property (nonatomic, strong) SASupplierScreeningView3 *screenView;

@end

@implementation SAInventoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshProduct) name:@"updateProduct" object:nil];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.screenView.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.screenView.hidden = YES;
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    if (self.stockPreviewListAPI == api) {
        SAInventoryListModel *listModel = [SAInventoryListModel mj_objectWithKeyValues:data[@"data"]];
        
        if (self.currentPage == 1) {
            self.dataMarr = [SAInventoryListRecods mj_objectArrayWithKeyValuesArray:data[@"data"][@"records"]];
            self.totalLab.text = [NSString stringWithFormat:@"       共%@种商品,库存数%@,总成本:%0.2lf",listModel.obj.totalProductCode,listModel.obj.totalProductNumber,listModel.obj.totalStockCost/100];
            
            NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
            if ([productViewCostPrice isEqualToString:@"1"]) {
                self.totalLab.text = [NSString stringWithFormat:@"       共%@种商品,库存数%@,总成本:******",listModel.obj.totalProductCode,listModel.obj.totalProductNumber];
            }
        }else {
            for (NSDictionary *dict in (NSArray *)data[@"data"][@"records"]) {
                SAInventoryListRecods *model = [SAInventoryListRecods mj_objectWithKeyValues:dict];
                [self.dataMarr addObject:model];
            }
        }
        if (self.dataMarr.count > 0) {
            self.topBtn.hidden = NO;
            self.lineNumLab.hidden = NO;
            self.lineborderView.hidden = NO;
            self.tipView.hidden = YES;
        }else {
            self.topBtn.hidden = YES;
            self.lineNumLab.hidden = YES;
            self.lineborderView.hidden = YES;
            self.tipView.hidden = NO;
        }
        [self.tableView reloadData];
    }else if (self.productGetAppConditionAPI == api) {
        [self.screenArray removeAllObjects];
        NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
        NSString *crossStoreStock = [NSString stringWithFormat:@"%@",menuList[3][@"perms"][@"crossStoreStock"]];
        for ( NSDictionary *dict in (NSArray *)data[@"data"]) {
            SAProductHomeScreeningModel *model = [SAProductHomeScreeningModel mj_objectWithKeyValues:dict];
            if ([model.name isEqualToString:@"店铺"] && [crossStoreStock isEqualToString:@"0"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.attrId == [SAValueManger shareInStance].userInfo.merchantId) {
                        list.check = YES;
                    }
                }
                [self.screenArray addObject:model];
            }
            if ([model.name isEqualToString:@"类别"]) {
                if (model.voList.count > 0) {
                    model.voList[0].check = YES;
                }
                [self.screenArray addObject:model];
            }else if ([model.name isEqualToString:@"状态"]) {
                if (model.voList.count > 1) {
                    model.voList[1].check = YES;
                }
                [self.screenArray addObject:model];
            }else if ([model.name isEqualToString:@"性别"]) {
                if (model.voList.count > 0) {
                    model.voList[0].check = YES;
                }
                [self.screenArray addObject:model];
            }else if ([model.name isEqualToString:@"季节"]) {
                if (model.voList.count > 0) {
                    model.voList[0].check = YES;
                }
                [self.screenArray addObject:model];
            }else if ([model.name isEqualToString:@"品牌"]) {
                if (model.voList.count > 0) {
                    model.voList[0].check = YES;
                }
                [self.screenArray addObject:model];
            }else if ([model.name isEqualToString:@"单位"]) {
                if (model.voList.count > 0) {
                    model.voList[0].check = YES;
                }
                [self.screenArray addObject:model];
            }
        }
        [self loadData];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    self.currentPage = 1;
    self.lineNumLab.text = @"共0款";
    [self.dataMarr removeAllObjects];
    [self.tableView reloadData];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataMarr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAInventoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAInventoryCell" forIndexPath:indexPath];
    cell.model = self.dataMarr[indexPath.row];
    cell.isRedBg = NO;
    WeakSelf
    cell.showImageBlock = ^{
        [weakSelf showImage:self.dataMarr[indexPath.row].imgList];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.moreView.hidden = YES;
    SAInventoryListRecods *model = self.dataMarr[indexPath.row];
    SAInventoryDetailVC *pushVC = [[SAInventoryDetailVC alloc] initWithProductId:model.productId AndMerchantId:self.stockPreviewListAPI.merchantId];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(71);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

#pragma mark - UITextField
- (void)textFieldEditChanged:(UITextField *)textField {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(searchProduct:) object:textField];
    [self performSelector:@selector(searchProduct:) withObject:textField afterDelay:0.3];
}

- (void)searchProduct:(UITextField *)textField {
    self.stockPreviewListAPI.productNameOrCode = textField.text;
    self.currentPage = 1;
    [self loadData];
}

-(void)refreshProduct {
    self.currentPage = 1;
    [self loadData];
}

#pragma mark - event
-(void)clickedMoreBtn {
    [self.view endEditing:YES];
    if (self.moreView.isHidden == YES) {
        self.moreView.hidden = NO;
    }else {
        self.moreView.hidden = YES;
    }
}

-(void)clickedSortingBtn {
    self.moreView.hidden = YES;
    SAInventorySortView *sortView = [SAInventorySortView shareCornView];
    [sortView loadData:@[@"默认", @"库存多", @"库存少", @"新款", @"旧款", @"爆款", @"滞销款"] AndSelectNum:self.sortNum];
    UIViewController *vc = nil;
    [sortView showView:vc];
    WeakSelf
    sortView.selectBlock = ^(NSInteger selectNum) {
        weakSelf.sortNum = selectNum;
        [weakSelf loadData];
    };
}

-(void)clickedScreeningBtn {
    self.moreView.hidden = YES;
//    [self prefersStatusBarHidden];
    __weak typeof(self) weakSelf = self;
    SASupplierScreeningView3 *screenView = [SASupplierScreeningView3 shareCornView];
    self.screenView = screenView;
    screenView.noAnimated = YES;
    screenView.delegate = self;
    [screenView loadData:self.screenArray SupplierName:self.providerName SupplierId:self.providerId];
    screenView.screeningBlock = ^(NSMutableArray *selectedArray, NSString *supplierName, NSString *supplierId) {
        [weakSelf.screenArray removeAllObjects];
        [weakSelf.screenArray addObjectsFromArray:selectedArray];
        if (supplierName.length > 0) {
            weakSelf.providerId = supplierId;
            weakSelf.providerName = supplierName;
        }else {
            weakSelf.providerId = @"";
            weakSelf.providerName = @"";
        }
        self.currentPage = 1;
        [self loadData];
    };
    [screenView showView:nil];
}

-(void)clickedTopBtn {
    if (self.dataMarr.count > 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        [self.tableView reloadData];
    }
}

-(void)clickedWarningBtn {
    self.moreView.hidden = YES;
    SAInventoryWarnVC *pushVC = [SAInventoryWarnVC new];
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedWarehousingBtn {
    NSMutableArray *menuList = [SAValueManger shareInStance].userInfo.menuList;
    NSString *isWarehousing = [NSString stringWithFormat:@"%@",menuList[2][@"isDelete"]];
    if ([isWarehousing isEqualToString:@"1"]) {
        [Prompt popPromptViewWithMsg:@"您没有入库权限,请联系店长添加" duration:2];
        return;
    }
    SAWarehousingVC *pushVC = [SAWarehousingVC new];
    WeakSelf
    pushVC.refreshBlock = ^{
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    };
    [self.navigationController pushViewController:pushVC animated:YES];
}

-(void)clickedScanBtn {
    [self.navigationController pushViewController:[[ScanHelper shareInstance] ScanVCWithStyle:notSquare qrResultCallBack:^(id result) {
        self.stockPreviewListAPI.providerId = @"";
        self.stockPreviewListAPI.unitId = @"";
        self.stockPreviewListAPI.order = @"";
        self.stockPreviewListAPI.state = @"1";
        self.stockPreviewListAPI.attrIds = @"";
        self.stockPreviewListAPI.productNameOrCode = result;
        self.searchTf.text = result;
        [self.stockPreviewListAPI sendRequestToServerWithActivityViewAndMask:GET];
        [self.navigationController popViewControllerAnimated:NO];
    }] animated:YES];
}

-(void)removeView {
    self.moreView.hidden = YES;
}

-(void)showImage:(NSArray *)imageList {
    self.imageList = imageList;
    if (self.imageList.count > 0) {
        NSMutableArray *imageMarr = [NSMutableArray array];
        for (NSString *url in self.imageList) {
            NSString *newUrl = [url stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
            NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_720",newUrl];
            [imageMarr addObject:imageUrl];
        }
        self.imageList = imageMarr;
        YBImageBrowser *browser = [YBImageBrowser new];
        browser.dataSourceArray = self.imageList;
        browser.dataSource = self;
        browser.currentPage = 0;
        [browser show];
    }else {
        [Prompt popPromptViewWithMsg:@"该商品没有图片" duration:2];
    }
}

//YBImageBrowserDataSource 代理实现赋值数据
- (NSInteger)yb_numberOfCellsInImageBrowser:(YBImageBrowser *)imageBrowser {
    return self.imageList.count;
}

- (id<YBIBDataProtocol>)yb_imageBrowser:(YBImageBrowser *)imageBrowser dataForCellAtIndex:(NSInteger)index {
    NSString *urlStr = [self.imageList objectAtIndex:index];
    YBIBImageData *data = [YBIBImageData new];
    data.imageURL = [NSURL URLWithString:urlStr];
    return data;
}

#pragma mark - initUI
-(void)initUI {
    self.title = @"";
    [self setBgImage:nil];
    [self addRightViews:@[self.topView]];
    [self.topView addSubview:self.warehousingBtn];
    [self.topView addSubview:self.searchTf];
    [self.topView addSubview:self.moreBtn];
    [self.topView addSubview:self.scanBtn];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.totalLab];
    [self.view addSubview:self.tipView];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.moreView];
    
    self.currentPage = 1;
    self.providerId = @"";
    self.providerName = @"";
    self.sortNum = 0;
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    }];
    MJRefreshAutoStateFooter *mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf.view endEditing:YES];
        weakSelf.currentPage ++;
        [weakSelf loadData];
    }];
    [mj_footer setTitle:@"已经到底啦~" forState:MJRefreshStateIdle];//设置闲置状态下不显示“点击或上拉加载更多”
    self.tableView.mj_footer = mj_footer;
    
    [self.productGetAppConditionAPI sendRequestToServerWithActivityViewAndMask:GET];
}

-(void)loadData {
    self.stockPreviewListAPI.pageSize = 20;
    self.stockPreviewListAPI.currentPage = self.currentPage;
    
    self.stockPreviewListAPI.providerId = @"";
    self.stockPreviewListAPI.unitId = @"";
    self.stockPreviewListAPI.order = @"";
    self.stockPreviewListAPI.state = @"1";
    self.stockPreviewListAPI.attrIds = @"";
    self.stockPreviewListAPI.merchantId = @([SAValueManger shareInStance].userInfo.merchantId).stringValue;
    
    if (self.providerId.length > 0) {
        self.stockPreviewListAPI.providerId = self.providerId;
    }
    
    if (self.screenArray.count > 0) {
        NSMutableArray *colorArray = [[NSMutableArray alloc] init];
        for (SAProductHomeScreeningModel *model in self.screenArray) {
            if ([model.name isEqualToString:@"店铺"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES && ![list.attrName isEqualToString:@"全部"]) {
                        self.stockPreviewListAPI.merchantId = [NSString stringWithFormat:@"%zd",list.attrId];
                    }
                }
            }else if ([model.name isEqualToString:@"单位"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES &&![list.attrName isEqualToString:@"全部"]) {
                        self.stockPreviewListAPI.unitId = [NSString stringWithFormat:@"%ld",list.attrId];
                    }
                }
            }else if ([model.name isEqualToString:@"排序"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES) {
                        self.sortNum = list.attrId;
                    }
                }
            }else if ([model.name isEqualToString:@"状态"]) {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES&&![list.attrName isEqualToString:@"全部"]) {
                        self.stockPreviewListAPI.state = [NSString stringWithFormat:@"%ld",list.attrId];
                    }
                }
                
            }else
            {
                for (SAProductHomeScreeningList *list in model.voList) {
                    if (list.check == YES&&![list.attrName isEqualToString:@"全部"]) {
                        [colorArray addObject:[NSString stringWithFormat:@"%ld",list.attrId]];
                    }
                }
                if (colorArray.count >0) {
                    self.stockPreviewListAPI.attrIds = [colorArray componentsJoinedByString:@","];
                    if ([self.stockPreviewListAPI.attrIds isEqualToString:@"0"]) {
                        self.stockPreviewListAPI.attrIds = @"";
                    }
                }
            }
        }
    }
    
    NSMutableArray *orderArray = [[NSMutableArray alloc] init];
    NSMutableDictionary *orderDict = [[NSMutableDictionary alloc] init];
    if (self.sortNum != 0) {
        switch (self.sortNum) {
            case 1:
                orderDict[@"key"] = @"newOrOldOrder";
                orderDict[@"value"] = @"desc";
                [orderArray addObject:orderDict];
                break;
            case 2:
                orderDict[@"key"] = @"newOrOldOrder";
                orderDict[@"value"] = @"asc";
                [orderArray addObject:orderDict];
                break;
            case 3:
                orderDict[@"key"] = @"burstOrStagnationOrder";
                orderDict[@"value"] = @"desc";
                [orderArray addObject:orderDict];
                break;
            case 4:
                orderDict[@"key"] = @"burstOrStagnationOrder";
                orderDict[@"value"] = @"asc";
                [orderArray addObject:orderDict];
                break;
            case 5:
                orderDict[@"key"] = @"stockOrder";
                orderDict[@"value"] = @"desc";
                [orderArray addObject:orderDict];
                break;
            case 6:
                orderDict[@"key"] = @"stockOrder";
                orderDict[@"value"] = @"asc";
                [orderArray addObject:orderDict];
                break;
            default:
                break;
        }
    }
    if (orderArray.count != 0) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:orderArray options:NSJSONWritingPrettyPrinted error:&error];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        
        self.stockPreviewListAPI.order = jsonString;
    }
    
    [self.stockPreviewListAPI sendRequestToServerWithActivityViewAndMask:GET];
}

#pragma mark - lazy
-(UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 44)];
    }
    return _topView;
}

-(UITextField *)searchTf {
    if (!_searchTf) {
        _searchTf = [[UITextField alloc] initWithFrame:SARectMake(100, 7, 225, 30)];
        _searchTf.placeholder = @"输入商品款号/名称/条码";
        _searchTf.backgroundColor = [UIColor whiteColor];
        _searchTf.layer.cornerRadius = kScaleNum(5);
        _searchTf.layer.masksToBounds = YES;
        _searchTf.delegate = self;
        _searchTf.font = kFont(14);
        _searchTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        UIView *leftView = [[UIView alloc] initWithFrame:SARectMake(0, 0, 26, 30)];
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:SARectMake(6, 7, 14, 16)];
        imageV.image = [UIImage imageNamed:@"shop_search"];
        [leftView addSubview:imageV];
        _searchTf.leftView = leftView;
        _searchTf.leftViewMode = UITextFieldViewModeAlways;
        [_searchTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTf;
}

-(UIButton *)moreBtn {
    if (!_moreBtn) {
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreBtn setImage:[UIImage imageNamed:@"Sale_screen"] forState:UIControlStateNormal];
        _moreBtn.frame = SARectMake(325, 0, 50, 44);
        [_moreBtn addTarget:self action:@selector(clickedMoreBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

-(UIButton *)warehousingBtn {
    if (!_warehousingBtn) {
        _warehousingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_warehousingBtn setTitle:@"入库" forState:UIControlStateNormal];
        [_warehousingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _warehousingBtn.titleLabel.font = kBFont(16);
        _warehousingBtn.frame = SARectMake(5, 0, 50, 44);
        [_warehousingBtn addTarget:self action:@selector(clickedWarehousingBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _warehousingBtn;
}

-(UIButton *)scanBtn {
    if (!_scanBtn) {
        _scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _scanBtn.frame = SARectMake(55, 0, 45, 44);
        [_scanBtn setImage:[UIImage imageNamed:@"home_scan"] forState:UIControlStateNormal];
        [_scanBtn addTarget:self action:@selector(clickedScanBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _scanBtn;
}

-(UIView *)moreView {
    if (!_moreView) {
        _moreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _moreView.backgroundColor = kColor(0, 0, 0, 0.3);
        _moreView.hidden = YES;
        _moreView.userInteractionEnabled = YES;
        
        UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Inventory_moreView"]];
        imageV.frame = SARectMake(290, StatusHeight - 6, 81, 93);
        [_moreView addSubview:imageV];
        imageV.userInteractionEnabled = YES;
        
        [imageV addSubview:self.sortingBtn];
        [imageV addSubview:self.sortingLine];
        [imageV addSubview:self.warningBtn];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
        [_moreView addGestureRecognizer:tap];
    }
    return _moreView;
}

-(UIButton *)sortingBtn {
    if (!_sortingBtn) {
        _sortingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sortingBtn.frame = SARectMake(0, 10, 81, 40);
        [_sortingBtn setTitle:@"库存筛选" forState:UIControlStateNormal];
        _sortingBtn.titleLabel.font = kFont(15);
        [_sortingBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [_sortingBtn addTarget:self action:@selector(clickedScreeningBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sortingBtn;
}

-(UIView *)sortingLine {
    if (!_sortingLine) {
        _sortingLine = [[UIView alloc] initWithFrame:SARectMake(0, 51, 71, 1)];
        _sortingLine.backgroundColor = kLineColor;
    }
    return _sortingLine;
}

-(UIButton *)warningBtn {
    if (!_warningBtn) {
        _warningBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _warningBtn.frame = SARectMake(0, 52, 81, 40);
        [_warningBtn setTitle:@"库存预警" forState:UIControlStateNormal];
        _warningBtn.titleLabel.font = kFont(14);
        [_warningBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
        [_warningBtn addTarget:self action:@selector(clickedWarningBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _warningBtn;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight + 30), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 30) - kScaleNum(49) - SafeFooterHeight) style:UITableViewStylePlain];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAInventoryCell class] forCellReuseIdentifier:@"SAInventoryCell"];
    }
    return _tableView;
}

-(NSMutableArray<SAInventoryListRecods *> *)dataMarr {
    if (!_dataMarr) {
        _dataMarr = [NSMutableArray array];
    }
    return _dataMarr;
}

-(NSMutableArray *)screenArray {
    if(!_screenArray) {
        _screenArray = [NSMutableArray array];
    }
    return _screenArray;
}

-(UIButton *)topBtn {
    if (!_topBtn) {
        _topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_topBtn setImage:[UIImage imageNamed:@"shop_topBtn"] forState:UIControlStateNormal];
        [_topBtn addTarget:self action:@selector(clickedTopBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topBtn;
}

-(UILabel *)lineNumLab {
    if (!_lineNumLab) {
        _lineNumLab = [[UILabel alloc] init];
        _lineNumLab.textColor = kRedColor;
        _lineNumLab.numberOfLines = 0;
        _lineNumLab.textAlignment = NSTextAlignmentCenter;
        _lineNumLab.font = kFont(12);
    }
    return _lineNumLab;
}

-(UIView *)lineborderView {
    if (!_lineborderView) {
        _lineborderView = [[UIView alloc] init];
        _lineborderView.layer.borderColor = kRedColor.CGColor;
        _lineborderView.layer.borderWidth = kScaleNum(1);
        _lineborderView.layer.cornerRadius = kScaleNum(3);
    }
    return _lineborderView;
}

-(SAStockPreviewListAPI *)stockPreviewListAPI {
    if (!_stockPreviewListAPI) {
        _stockPreviewListAPI = [SAStockPreviewListAPI apiWithDelegate:self];
    }
    return _stockPreviewListAPI;
}

-(SAproductGetAppConditionAPI *)productGetAppConditionAPI {
    if (!_productGetAppConditionAPI) {
        _productGetAppConditionAPI = [SAproductGetAppConditionAPI apiWithDelegate:self];
    }
    return _productGetAppConditionAPI;
}

-(UIView *)tipView {
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight) - kScaleNum(49) - SafeFooterHeight)];
        _tipView.backgroundColor = kLineColor;
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Shop_search2"]];
        icon.frame = SARectMake(124, 150, 138, 83);
        [_tipView addSubview:icon];
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, icon.maxY + kScaleNum(20), _tipView.width, kScaleNum(20))];
        titleLab.text = @"暂无相关信息";
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = kGrayColor;
        [_tipView addSubview:titleLab];
    }
    return _tipView;
}

-(UILabel *)totalLab {
    if (!_totalLab) {
        _totalLab = [[UILabel alloc] initWithFrame:SARectMake(0, StatusHeight, kBaseScreenWidth, 30)];
        _totalLab.textColor = kLightgGrayColor;
        _totalLab.textAlignment = NSTextAlignmentLeft;
        _totalLab.backgroundColor = kTextBgColor;
        _totalLab.font = kFont(14);
    }
    return _totalLab;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    HDLog(@"%@ 控制器已经被释放",NSStringFromClass([self class]));
}


@end
